package org.example.controller;

import lombok.extern.slf4j.Slf4j;
import org.example.bean.enums.SSEMsgType;
import org.example.utils.SSEServer;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

@Slf4j
@RequestMapping("sse")
@RestController
public class SSEController {

    @GetMapping(path = "connect",produces = {MediaType.TEXT_EVENT_STREAM_VALUE})
    public SseEmitter connect(@RequestParam String userId){
        log.info("用户{}连接成功",userId);
        return SSEServer.connect(userId);
    }


    @GetMapping(path = "sendMessage")
    public Object sendMessage(@RequestParam String userId,@RequestParam String massage){
        SSEServer.sendMassage(userId,massage, SSEMsgType.MASSAGE);
        return "ok";
    }

}
